using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

using WorkflowProcesses = ScenarioFramework.WorkflowEngine.DataModel.WorkflowProcesses;
using WorkflowRelevantData = ScenarioFramework.WorkflowEngine.DataModel.WorkflowRelevantData;
using WorkflowParticipantsData = ScenarioFramework.WorkflowEngine.DataModel.WorkflowParticipantsData;

using WAPI = ScenarioFramework.WAPI;

namespace Scenario.Modeler.Forms
{
	/// <summary>
	/// Descrizione di riepilogo per ProcessesList.
	/// </summary>
	public class ProcessesList : System.Windows.Forms.Form
	{
		/// <summary>
		/// Variabile di progettazione necessaria.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public WorkflowProcesses.XMLWorkflows xmlWorkflows;
		private System.Windows.Forms.ListView lwProcesses;
		private System.Windows.Forms.Panel panel1;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.RadioButton OptProcessList;
		private System.Windows.Forms.RadioButton optOneProcess;
		private System.Windows.Forms.Button btnLoad;
		private System.Windows.Forms.TextBox txtProcessName;
		private System.Windows.Forms.TextBox txtID;
		public WorkflowProcesses.XMLWorkflow XMLWorkflow;

		public ProcessesList()
		{
			//
			// Necessario per il supporto di Progettazione Windows Form
			//
			InitializeComponent();

			this.MakeGrid();
		}

		private void MakeGrid()
		{
			ColumnHeader ch = new ColumnHeader();
			ch.Text = "ProcessId";
			ch.TextAlign = HorizontalAlignment.Right;
			ch.Width = 100;
			lwProcesses.Columns.Add(ch);

			ch = new ColumnHeader();
			ch.Text = "Name";
			ch.TextAlign = HorizontalAlignment.Left;
			ch.Width = 100;
			lwProcesses.Columns.Add(ch);

			ch = new ColumnHeader();
			ch.Text = "Description";
			ch.TextAlign = HorizontalAlignment.Left;
			ch.Width = 100;
			lwProcesses.Columns.Add(ch);

			ch = new ColumnHeader();
			ch.Text = "Status";
			ch.TextAlign = HorizontalAlignment.Left;
			ch.Width = 100;
			lwProcesses.Columns.Add(ch);
		}

		public WorkflowProcesses.XMLWorkflows XMLWorkflows 
		{
			get { return xmlWorkflows;}
			set 
			{
				xmlWorkflows = value;

				WorkflowProcesses.XMLWorkflow workflow = null;
				for ( int i=0; i<xmlWorkflows.Count;i++)
				{
					workflow = (WorkflowProcesses.XMLWorkflow)xmlWorkflows[i];
					ListViewItem item = new ListViewItem();
					item.Text = workflow.ProcessId.ToString();
					item.SubItems.Add(workflow.Name);
					item.SubItems.Add(workflow.Description);
					item.SubItems.Add(workflow.Status);
					this.lwProcesses.Items.Add(item);
				}
			}
		}
			

		/// <summary>
		/// Pulire le risorse in uso.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Codice generato da Progettazione Windows Form
		/// <summary>
		/// Metodo necessario per il supporto della finestra di progettazione. Non modificare
		/// il contenuto del metodo con l'editor di codice.
		/// </summary>
		private void InitializeComponent()
		{
			this.lwProcesses = new System.Windows.Forms.ListView();
			this.panel1 = new System.Windows.Forms.Panel();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.txtID = new System.Windows.Forms.TextBox();
			this.btnLoad = new System.Windows.Forms.Button();
			this.txtProcessName = new System.Windows.Forms.TextBox();
			this.optOneProcess = new System.Windows.Forms.RadioButton();
			this.OptProcessList = new System.Windows.Forms.RadioButton();
			this.panel1.SuspendLayout();
			this.groupBox1.SuspendLayout();
			this.SuspendLayout();
			// 
			// lwProcesses
			// 
			this.lwProcesses.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.lwProcesses.Dock = System.Windows.Forms.DockStyle.Bottom;
			this.lwProcesses.Location = new System.Drawing.Point(0, 192);
			this.lwProcesses.Name = "lwProcesses";
			this.lwProcesses.Size = new System.Drawing.Size(856, 592);
			this.lwProcesses.TabIndex = 0;
			this.lwProcesses.View = System.Windows.Forms.View.Details;
			this.lwProcesses.DoubleClick += new System.EventHandler(this.lwProcesses_DoubleClick);
			// 
			// panel1
			// 
			this.panel1.Controls.Add(this.groupBox1);
			this.panel1.Dock = System.Windows.Forms.DockStyle.Top;
			this.panel1.Location = new System.Drawing.Point(0, 0);
			this.panel1.Name = "panel1";
			this.panel1.Size = new System.Drawing.Size(856, 168);
			this.panel1.TabIndex = 1;
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.txtID);
			this.groupBox1.Controls.Add(this.btnLoad);
			this.groupBox1.Controls.Add(this.txtProcessName);
			this.groupBox1.Controls.Add(this.optOneProcess);
			this.groupBox1.Controls.Add(this.OptProcessList);
			this.groupBox1.Location = new System.Drawing.Point(8, 16);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(832, 136);
			this.groupBox1.TabIndex = 0;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Load";
			// 
			// txtID
			// 
			this.txtID.Enabled = false;
			this.txtID.Location = new System.Drawing.Point(232, 96);
			this.txtID.Name = "txtID";
			this.txtID.Size = new System.Drawing.Size(48, 22);
			this.txtID.TabIndex = 4;
			this.txtID.Text = "<Id>";
			// 
			// btnLoad
			// 
			this.btnLoad.Location = new System.Drawing.Point(744, 88);
			this.btnLoad.Name = "btnLoad";
			this.btnLoad.Size = new System.Drawing.Size(72, 40);
			this.btnLoad.TabIndex = 3;
			this.btnLoad.Text = "Load";
			this.btnLoad.Click += new System.EventHandler(this.btnLoad_Click);
			// 
			// txtProcessName
			// 
			this.txtProcessName.Enabled = false;
			this.txtProcessName.Location = new System.Drawing.Point(16, 96);
			this.txtProcessName.Name = "txtProcessName";
			this.txtProcessName.Size = new System.Drawing.Size(208, 22);
			this.txtProcessName.TabIndex = 2;
			this.txtProcessName.Text = "<ProcessName>";
			// 
			// optOneProcess
			// 
			this.optOneProcess.Location = new System.Drawing.Point(16, 72);
			this.optOneProcess.Name = "optOneProcess";
			this.optOneProcess.TabIndex = 1;
			this.optOneProcess.Text = "Process";
			this.optOneProcess.CheckedChanged += new System.EventHandler(this.optOneProcess_CheckedChanged);
			// 
			// OptProcessList
			// 
			this.OptProcessList.Checked = true;
			this.OptProcessList.Location = new System.Drawing.Point(16, 32);
			this.OptProcessList.Name = "OptProcessList";
			this.OptProcessList.Size = new System.Drawing.Size(120, 16);
			this.OptProcessList.TabIndex = 0;
			this.OptProcessList.TabStop = true;
			this.OptProcessList.Text = "All Processes";
			// 
			// ProcessesList
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
			this.ClientSize = new System.Drawing.Size(856, 784);
			this.Controls.Add(this.panel1);
			this.Controls.Add(this.lwProcesses);
			this.Name = "ProcessesList";
			this.Text = "ProcessesList";
			this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
			this.panel1.ResumeLayout(false);
			this.groupBox1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		private void optOneProcess_CheckedChanged(object sender, System.EventArgs e)
		{
			if ( optOneProcess.Checked == true )
			{
				this.txtID.Enabled = true;
				this.txtProcessName.Enabled  = true;
			}
			else
			{
				this.txtID.Enabled = false;
				this.txtProcessName.Enabled = false;
			}
		}

		private void LoadProcess(long lProcessId)
		{
			WAPI.Response wapiResponse = new WAPI.Response();
			string sXML = string.Empty;
			sXML = WAPI.Process.GetProcessInstance ( ref wapiResponse, lProcessId);
			XMLWorkflow = new WorkflowProcesses.XMLWorkflow();
			XMLWorkflow.Parse (sXML);
		}

		private void btnLoad_Click(object sender, System.EventArgs e)
		{
			if ( optOneProcess.Checked == true )
			{
				if ( this.txtID.Text != string.Empty ) 
				{
					this.LoadProcess (Int32.Parse ( this.txtID.Text ));
					this.Close();
				}
				else if ( this.txtProcessName.Text != string.Empty ) 
				{
					MessageBox.Show ("TODO");
					
				}
				else
				{
					MessageBox.Show ("It's mandatory select process Id or name");
					
				}
			}
			else
			{
				WAPI.Response wapiResponse = new WAPI.Response();
				string sXML = string.Empty;
				sXML = WAPI.Process.GetProcesses(ref wapiResponse);

				if ( wapiResponse.iCode ==  0)
				{
					WorkflowProcesses.XMLWorkflows xmlWorkflows = new WorkflowProcesses.XMLWorkflows();
					xmlWorkflows.Parse (sXML);

					this.XMLWorkflows = xmlWorkflows;
				}
				else
				{
					MessageBox.Show ("Impossible to retrieve Processes List. " + wapiResponse.sMsg );
				}
			}
		}

		private void lwProcesses_DoubleClick(object sender, System.EventArgs e)
		{
			this.LoadProcess (Int32.Parse (this.lwProcesses.SelectedItems[0].SubItems[0].Text));
			this.Close();
		}

	}
}
